package com.tbox.tbsdk;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by rui.yang
 * on 2020/12/3
 */
class ByteUtil {

    /// 合并 bytes
    public static byte[] merge(byte[]... values) {
        int byteLength = 0;
        for (int i = 0; i < values.length; i++) {
            byteLength += values[i].length;
        }
        byte[] all_byte = new byte[byteLength];
        int count = 0;
        for (int i = 0; i < values.length; i++) {
            byte[] b = values[i];
            System.arraycopy(b, 0, all_byte, count, b.length);
            count += b.length;
        }
        return all_byte;
    }

    /// 截取
    public static byte[] subByte(byte[] b, int off, int length){
        byte[] result = new byte[length];
        System.arraycopy(b, off, result, 0, length);
        return result;
    }

    /// 计算 bcc
    public static byte bcc(byte[] data, int start, int end) {
        byte BCC[] = new byte[1];
        for (int i = start; i < data.length; i++) {
            if (i == end) {
                break;
            }
            BCC[0] = (byte) (BCC[0] ^ data[i]);
        }
        return (byte)(BCC[0] & 0xFF);
    }
}
